package com.ruoyi.article.controller;

import com.ruoyi.article.domain.Article;
import com.ruoyi.article.mapper.ClassifyMapper;
import com.ruoyi.article.service.ArticleService;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/article")
public class ArticleController extends BaseController {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private ClassifyMapper classifyMapper;



    @GetMapping("/hello")
    public String Hello(){
        return "Hello World";
    }


    @Log(title = "文章管理", businessType = BusinessType.INSERT)
    // TODO:
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Article article)
    {
        if (!articleService.checkArticleUnique(article))
        {
            return error("新增文章'" +article.getName() + "'失败，文章已存在");
        }
        article.setCreateBy(SecurityUtils.getUsername());
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("a", 1);  // 设置要增加的值
        paramMap.put("name", article.getType()); // 设置筛选条件
        classifyMapper.updateClassifyNumber(paramMap);
        return toAjax(articleService.insertArticle(article));
    }


   /**   list*/
    @GetMapping("/list")
    public TableDataInfo list(Article article)
    {
        startPage();
        List<Article> list = articleService.selectArticleList(article);
        return getDataTable(list);
    }

    /** export*/
    @Log(title = "文章管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Article article)
    {
        List<Article> list = articleService.selectArticleList(article);
        ExcelUtil<Article> util = new ExcelUtil<Article>(Article.class);
        util.exportExcel(response, list, "文章数据");
    }

    /** getinfo by id */

//    @RequiresPermissions("article:article:query")
    @GetMapping(value = {  "/{articleId}" })
    public AjaxResult getInfo(@PathVariable(value = "articleId", required = false) Long articleId) {
        return success(articleService.selectArticleById(articleId));
    }



   /** delete by id */
    @Log(title = "文章管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{articleId}")
    public AjaxResult remove(@PathVariable Long articleId)
    {
        Article article = articleService.selectArticleById(articleId);
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("a", -1);  // 设置要增加的值
        paramMap.put("name", article.getType()); // 设置筛选条件
        classifyMapper.updateClassifyNumber(paramMap);
        return toAjax(articleService.deleteArticleById(articleId));
    }

    @Log(title = "文章管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Article article)
    {
        Article article1 = articleService.selectArticleById(article.getArticleId());
        if(article.getType()!=article1.getType())
        {

            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("a", +1);  // 设置要增加的值
            paramMap.put("name", article.getType()); // 设置筛选条件
            classifyMapper.updateClassifyNumber(paramMap);

            paramMap.put("a", -1);  // 设置要减少的值
            paramMap.put("name", article1.getType()); // 设置筛选条件
            classifyMapper.updateClassifyNumber(paramMap);
        }
        return toAjax(articleService.updateArticle(article));
    }




}
